//
//  RCWelcomeController.swift
//  RC-微博0305
//
//  Created by 任淳 on 16/3/10.
//  Copyright © 2016年 ReverChun. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class RCWelcomeController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view = BackGroundImage
        
        view.addSubview(iconImage)
        view.addSubview(welcomeLab)
        
        setContranits()
        
    }
    
    //一定要在viewDidAppear里面实现动画  不然看不到效果
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)
        
        //实现头像弹上来
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 6.9, options: [], animations: { () -> Void in
            //更改头像的约束 去实现动画效果
            self.iconImage.snp_updateConstraints(closure: { (make) -> Void in
                make.centerY.equalTo(self.BackGroundImage).offset(-200)
                 })
            //改变好约束以后 强制刷新来实现动画
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                //完成之后执行的操作
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    //头像动画完全出现之后，欢迎语再出来
                    self.welcomeLab.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        //欢迎语出来以后 就跳转到主页 ==============================第二次跳转进去
                        //UIApplication.sharedApplication().keyWindow?.rootViewController = RCMainController()
                    //用户消息通知机制来告诉AppDelegate该进入哪个页面
                    NSNotificationCenter.defaultCenter().postNotificationName("chooseController", object: nil)
                })
                
        }
        
    }
    
    //设置约束
    func setContranits() {
        
        iconImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(BackGroundImage)
            make.centerY.equalTo(BackGroundImage).offset(180)
            make.width.height.equalTo(90)
        }
        
        welcomeLab.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage)
            make.top.equalTo(iconImage.snp_bottom).offset(20)
            make.width.equalTo(300)
        }
        
        
        
    }
    
    
    lazy var BackGroundImage:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    lazy var iconImage:UIImageView = {
        
        let url = NSURL(string: RCUserModel.sharedUserModel.avatar_large ?? "")
        let iconImage = UIImageView()
        iconImage.sd_setImageWithURL(url)
        //把头像切成圆的
        iconImage.layer.cornerRadius = 45
        iconImage.clipsToBounds = true
        iconImage.layer.borderWidth = 3
        iconImage.layer.borderColor = UIColor.redColor().CGColor
        
        iconImage.sizeToFit()
        
        return iconImage
        
    }()
    
    lazy var welcomeLab:UILabel = {
        
        let lab = UILabel()
        lab.text = (RCUserModel.sharedUserModel.name ?? "") + "  欢迎回来"
        lab.textColor = UIColor.purpleColor()
        lab.font = UIFont.systemFontOfSize(16)
        lab.alpha = 0
        lab.textAlignment = .Center
        lab.sizeToFit()
        return lab
    }()
    
}
